package com.unidt.www.module.third.doc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unidt.www.framework.common.pojo.PageResult;
import com.unidt.www.framework.common.util.object.ConvertUtil;
import com.unidt.www.framework.mybatis.core.util.PageFactory;
import com.unidt.www.module.third.doc.dao.ProjectBusInfoMapper;
import com.unidt.www.module.third.doc.dao.ProjectPlanMapper;
import com.unidt.www.module.third.doc.domain.ProjectPlanDo;
import com.unidt.www.module.third.doc.form.ProjectQueryForm;
import com.unidt.www.module.third.doc.service.IProjectBusInfoService;
import com.unidt.www.module.third.doc.vo.ProjectBusInfoVo;
import com.unidt.www.module.third.doc.vo.ProjectPlanVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author sk
 * @Description:
 * @Date 2022/11/25 12:13
 **/
@Service
public class ProjectBusInfoServiceImpl implements IProjectBusInfoService {

    @Resource
    ProjectBusInfoMapper projectBusInfoMapper;

    @Resource
    ProjectPlanMapper projectPlanMapper;

    @Override
    public PageResult<ProjectBusInfoVo> queryPage(ProjectQueryForm queryForm) {
        IPage iPage = projectBusInfoMapper.queryPage(PageFactory.createPage(queryForm), queryForm);
         List<ProjectBusInfoVo> infoVoList =  iPage.getRecords();

         if(!infoVoList.isEmpty()){
             List<Long> projectIds = infoVoList.stream().map(d->d.getId()).collect(Collectors.toList());
             List<ProjectPlanDo> list = projectPlanMapper.getPlansByBusIds(projectIds);
             List<ProjectPlanVo> projectPlanVos =  ConvertUtil.convertList(list, ProjectPlanVo.class);
             if(!projectPlanVos.isEmpty()){
                 Map<Long,List<ProjectPlanVo>> listMap =
                         projectPlanVos.stream().collect(Collectors.groupingBy(ProjectPlanVo::getProjectBusId));
                 infoVoList.forEach(info->{
                     List<ProjectPlanVo> planVoList = listMap.get(info.getId());
                     info.setPlanList(planVoList);
                 });
             }
         }
        return new PageResult(iPage.getRecords(), iPage.getTotal());
    }

}
